package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.mongo.RecommendUser;
import com.tanhua.model.mongo.UserLike;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class RecommendUserApiImpl implements RecommendUserApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    //查询今日佳人
    public RecommendUser queryWithMaxScore(Long toUserId) {

        //根据toUserId查询，根据评分score排序，获取第一条
        //构建Criteria
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        //构建Query对象
        Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("score")))
                .limit(1);
        //调用mongoTemplate查询

        return mongoTemplate.findOne(query, RecommendUser.class);
    }

    @Override
    public PageResult queryRecommendUserList(Integer page, Integer pagesize, Long toUserId) {
        //根据userId获取Query对象
        Query query = new Query(Criteria.where("toUserId").is(toUserId));
        //根据条件查询RecommendUser总数
        long count = mongoTemplate.count(query, RecommendUser.class);
        //补充分页参数
        query.with(Sort.by(Sort.Order.desc("score"))).limit((page - 1) * pagesize).skip(pagesize);
        //查询分页数据列表
        List<RecommendUser> list = mongoTemplate.find(query, RecommendUser.class);
        //构建返回值
        return new PageResult(page, pagesize, count, list);
        /*PageResult pr=new PageResult();


        
        return pr;*/
    }

    @Override
    public RecommendUser queryByUserId(Long id, Long userId) {
        Query query = Query.query(Criteria.where("userId").is(id).and("toUserId").is(userId));
        //根据条件查询
        RecommendUser user = mongoTemplate.findOne(query, RecommendUser.class);
        //判断是否为空
        if (user == null) {
            //为空,新建对象并赋值
            user = new RecommendUser();
            user.setUserId(id);
            user.setScore(95d);
            user.setToUserId(userId);
        }
        //不为直接返回user
        return user;
    }

    @Override
    public List<RecommendUser> queryCardsList(Long userId, int i) {
        //首先,根据当前用户id,获取喜欢和不喜欢的用户
        Query query=Query.query(Criteria.where("userId").is(userId));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        List<Long> likeIds = CollUtil.getFieldValues(userLikes, "likeUserId", Long.class);
        //调用查询用户,排除已经喜欢和不喜欢的用户
        Criteria criteria = Criteria.where("toUserId").is(userId).and("userId").nin(likeIds);
        //使用统计函数,随机获取10条数据
        TypedAggregation<RecommendUser> newAggregation = TypedAggregation.newAggregation(RecommendUser.class,
                Aggregation.match(criteria),
                Aggregation.sample(i));
        AggregationResults<RecommendUser> aggregate = mongoTemplate.aggregate(newAggregation, RecommendUser.class);
        return aggregate.getMappedResults();
    }
}
